package com.dragon.concurrent.demo.test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 多线程并发编程演示案例3
 *
 * @author dragon
 * @since 2019/11/20
 */
public class ConcurrencyTest3 {

    private static AtomicInteger a = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        int count = 200000;
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < count; i++) {
                a.incrementAndGet();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < count; i++) {
                a.incrementAndGet();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("a:" + a);
    }

    static class ReorderingDemo {
        static int x = 0, y = 0, a = 0, b = 0;

        public static void main(String[] args) throws Exception {
            for (int i = 0; i < 100; i++) {
                x = y = a = b = 0;
                Thread one = new Thread() {

                    @Override
                    public void run() {
                        a = 1;
                        x = b;
                    }
                };
                Thread two = new Thread() {

                    @Override
                    public void run() {
                        b = 1;
                        y = a;
                    }
                };
                one.start();
                two.start();
                one.join();
                two.join();
                System.out.println(x + " " + y);
            }
        }
    }

}

